﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace RoadTeam.Pub
{
  public  class ForEachControl
    {
        public static void GetConTrolVal(ref string StrValue, string constrolname, Control contr)
        {
            foreach (Control ct in contr.Controls)
            {
                if (ct.Controls.Count > 0) GetConTrolVal(ref  StrValue, constrolname, ct);
                if (ct.ID == constrolname)
                {
                    String StrControlType = ct.GetType().ToString().ToUpper();
                    switch (StrControlType)
                    {
                        case "SYSTEM.WEB.UI.WEBCONTROLS.TEXTBOX"://TextBox
                            StrValue = ((TextBox)ct).Text;
                            break;
                        case "SYSTEM.WEB.UI.WEBCONTROLS.RADIOBUTTONLIST"://单选
                            RadioButtonList radiolist = ((RadioButtonList)ct);
                            foreach (ListItem li in radiolist.Items)
                            {
                                if (li.Selected) { StrValue = li.Value; break; }
                            }
                            break;
                        //case "SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOXLIST"://多项
                        //    break;
                        case "SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOX": //单选CHECKBOX
                            StrValue = ((CheckBox)ct).Checked.ToString();
                            break;
                        case "SYSTEM.WEB.UI.WEBCONTROLS.DROPDOWNLIST"://下拉
                            DropDownList ddl = ((DropDownList)ct);
                            if (ddl.Items.Count == 0 || ddl.SelectedValue == null || String.IsNullOrEmpty(ddl.SelectedValue))
                                StrValue = String.Empty;
                            else
                                StrValue = ddl.SelectedValue;
                            break;
                        case "SYSTEM.WEB.UI.WEBCONTROLS.LISTBOX"://列表
                            ListBox libox = (ListBox)ct;
                            if (libox.Items.Count == 0 || libox.SelectedValue == null || String.IsNullOrEmpty(libox.SelectedValue))
                                StrValue = String.Empty;
                            else
                                StrValue = libox.SelectedValue;
                            break;
                        case "SYSTEM.WEB.UI.WEBCONTROLS.LABEL"://标签
                            StrValue = ((Label)ct).Text;
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTRADIOBUTTON": //单选
                            StrValue = ((System.Web.UI.HtmlControls.HtmlInputRadioButton)ct).Checked.ToString();
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTCHECKBOX"://多选
                            System.Web.UI.HtmlControls.HtmlInputCheckBox chb = (HtmlInputCheckBox)ct;
                            StrValue = chb.Checked.ToString();
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTHIDDEN"://隐藏
                            System.Web.UI.HtmlControls.HtmlInputHidden hidden = (HtmlInputHidden)ct;
                            StrValue = hidden.Value.ToString();
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTTEXT"://单行
                            System.Web.UI.HtmlControls.HtmlInputText inptxt = (HtmlInputText)ct;
                            StrValue = inptxt.Value.ToString();
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLTEXTAREA"://多行
                            System.Web.UI.HtmlControls.HtmlTextArea inptxtArea = (HtmlTextArea)ct;
                            StrValue = inptxtArea.Value.ToString();
                            break;
                        case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLSELECT"://列表
                            System.Web.UI.HtmlControls.HtmlSelect select = (HtmlSelect)ct;
                            if (select.SelectedIndex > -1)
                                StrValue = select.Items[select.SelectedIndex].Value;
                            else
                                StrValue = String.Empty;
                            break;
                        default:
                            StrValue = String.Empty;
                            break;
                    }
                }
            }
        }
    }
}
